package com.xysq.util;

import android.text.TextUtils;

/**
 * 对订单信息进行字符拼接
 */
public final class AlipayOrderInfoBuilder {

    private static final String TAG = "AlipayOrderInfoBuilder";

    private static final String PARAM_FORMAT = "&%s=\"%s\"";

    private static final String DEFAULT_CHARSET = "utf-8";

    private static final String SERVICE = "mobile.securitypay.pay";

    private String mOutTradeNo;
    private String mSubject;
    private String mBody;
    private String mTotalFee;
    private String mNotifyUrl;

    private StringBuilder mAlipayInfoBuilder;

    public AlipayOrderInfoBuilder() {
        mAlipayInfoBuilder = new StringBuilder();
    }

    public void setOutTradeNo(String outTradeNo) {
        mOutTradeNo = outTradeNo;
    }

    public void setBody(String body) {
        mBody = body;
    }

    public void setSubject(String subject) {
        mSubject = subject;
    }

    public void setTotalFee(String totalFee) {
        mTotalFee = totalFee;
    }

    public void setNotifyUrl(String notifyUrl) {
        mNotifyUrl = notifyUrl;
    }

    public String build() {
        mAlipayInfoBuilder.setLength(0); // Clean the builder
        appendRequiredParam(AlipayParam.PARTNER, Keys.DEFAULT_PARTNER);
        appendRequiredParam(AlipayParam.SELLER_ID, Keys.DEFAULT_SELLER);
        appendRequiredParam(AlipayParam.OUT_TRADE_NO, mOutTradeNo);
        appendRequiredParam(AlipayParam.SUBJECT, mSubject);
        appendRequiredParam(AlipayParam.BODY, mBody);
        appendRequiredParam(AlipayParam.TOTAL_FEE, mTotalFee);
        appendRequiredParam(AlipayParam.NOTIFY_URL, mNotifyUrl);
        appendRequiredParam(AlipayParam.SERVICE, SERVICE);
        appendRequiredParam(AlipayParam.PAYMENT_TYPE, "1");
        appendRequiredParam(AlipayParam.INPUT_CHARSET, DEFAULT_CHARSET);
        appendRequiredParam(AlipayParam.IT_B_PAY, "30m");
        appendRequiredParam(AlipayParam.RETURN_URL, "");
        mAlipayInfoBuilder.deleteCharAt(0);
        return mAlipayInfoBuilder.toString();
    }

    private void appendRequiredParam(String paramName, String paramValue) {
        if (TextUtils.isEmpty(paramValue)) {
            return;
        }
        mAlipayInfoBuilder.append(String.format(PARAM_FORMAT, paramName, paramValue));
    }

}
